package com.lcb.manager.web;

import com.alibaba.fastjson.JSON;
import com.lcb.manager.configuration.Config;
import com.lcb.manager.util.MediaUploadUtil;
import com.lcb.manager.vo.CompanyVo;
import com.lcb.manager.vo.Json;
import com.lcb.manager.entity.Company;
import com.lcb.manager.repository.CompanyRepository;
import com.lcb.manager.util.RandomUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.Date;
import java.util.List;

@Controller
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyRepository companyRepository;

    @RequestMapping(value = "/")
    public String index() {
        return "company";
    }

    @ResponseBody
    @PostMapping("/add")
    public String add(@RequestParam("files") MultipartFile[] files,
                      @RequestParam("type") int type,
                      @RequestParam("date") String date,
                      @RequestParam("title") String title,
                      @RequestParam("content") String content) {
        Json result = new Json();
        int fileLen = files.length;
        if (date.isEmpty() || title.isEmpty() || content.isEmpty()) {
            result.code = 1;
            result.msg = "请勿提交空数据！";
            return JSON.toJSONString(result);
        } else if (fileLen == 0) {
            result.code = 1;
            result.msg = "请选择图片！";
            return JSON.toJSONString(result);
        } else if (type == 2 && fileLen > 1) {
            result.code = 1;
            result.msg = "zuiduoshangchuanyigeshiping！";
            return JSON.toJSONString(result);
        } else {
            Company company = new Company();
            company.setNum(0);
            company.setDate(Date.valueOf(date));
            company.setTitle(title);
            company.setContent(content);
            company.setType(type);
            if (type == 1) {
                List<String> imageNameList = MediaUploadUtil.uploadImages(files);
                company.setPictures(JSON.toJSONString(imageNameList));
            } else {
                String videoName = MediaUploadUtil.uploadVideo(files);
                company.setVideo(videoName);
            }
            companyRepository.save(company);
            result.code = 0;
            result.msg = "提交成功！";
            return JSON.toJSONString(result);
        }
    }

    @ResponseBody
    @PostMapping("/edit")
    public String edit(@RequestParam("files") MultipartFile[] files,
                       @RequestParam("type") int type,
                       @RequestParam("id") int id,
                       @RequestParam("date") String date,
                       @RequestParam("title") String title,
                       @RequestParam("content") String content) throws IOException {
        Json result = new Json();

        int fileLen = files.length;
        if (date.isEmpty() || title.isEmpty() || content.isEmpty()) {
            result.code = 1;
            result.msg = "请勿提交空数据！";
            return JSON.toJSONString(result);
        } else if (fileLen == 0) {
            result.code = 1;
            result.msg = "请选择图片！";
            return JSON.toJSONString(result);
        } else {
            Company company = companyRepository.findById(id).orElse(null);
            if (company != null) {
                company.setDate(Date.valueOf(date));
                company.setTitle(title);
                company.setContent(content);
                company.setType(type);
                if (type == 1) {
                    List<String> imageNameList = MediaUploadUtil.uploadImages(files);
                    company.setPictures(JSON.toJSONString(imageNameList));
                } else {
                    String videoName = MediaUploadUtil.uploadVideo(files);
                    company.setVideo(videoName);
                }
                companyRepository.save(company);
            }
            result.code = 0;
            result.msg = "提交成功！";
            return JSON.toJSONString(result);
        }
    }

    @ResponseBody
    @GetMapping("/edit_num")
    public String editNum(@RequestParam("id") int id,
                          @RequestParam("num") int num) throws IOException {
        Json result = new Json();
        Company company = companyRepository.findById(id).orElse(null);
        if (company != null) {
            company.setNum(num);
            companyRepository.save(company);
        }
        result.code = 0;
        result.msg = "提交成功！";
        return JSON.toJSONString(result);
    }

    @ResponseBody
    @RequestMapping(value = "/delete")
    public String delete(@RequestParam int id) {
        Json result = new Json();
        companyRepository.deleteById(id);
        result.code = 0;
        result.msg = "删除成功！";
        return JSON.toJSONString(result);
    }

    @ResponseBody
    @RequestMapping(value = "/list")
    public String getCompanyList(@RequestParam(defaultValue = "") String date, @RequestParam int page, @RequestParam int limit) {
        Json result = new Json();
        Pageable pageable = PageRequest.of(page - 1, limit);
        Page<Company> pageResult;
        if (!date.isEmpty()) {
            String year = date.split("-")[0];
            String month = date.split("-")[1];
            pageResult = companyRepository.findByDateOrderByDateAsc(year, month, pageable);
        } else {
            pageResult = companyRepository.findByOrderByDateAsc(pageable);
        }
        result.code = 0;
        result.count = (int) pageResult.getTotalElements();
        result.data = pageResult.getContent();
        return JSON.toJSONStringWithDateFormat(result, "yyyy-MM-dd");
    }

    @RequestMapping(value = "/get_empty")
    public String getEmpty(ModelMap modelMap) {
        modelMap.put("title", "企业简介输入");
        modelMap.put("isEdit", false);
        return "_content";
    }

    @RequestMapping(value = "/get_one")
    public String getOne(ModelMap modelMap, @RequestParam int id) {
        Company company = companyRepository.findById(id).orElse(null);
        CompanyVo vo = CompanyVo.fromEntity(company);
        modelMap.put("title", "企业简介输入");
        modelMap.put("isEdit", true);
        if (vo != null) {
            modelMap.put("vo", vo);
        }
        return "_content";
    }


}